package com.shamar.blog.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by lxy on 16/12/10.
 */
public class DateUtils {

    private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);

    private static final ZoneId zoneId = ZoneId.systemDefault();

    /**
     * 返回系统当前的完整日期时间 <br>
     * 格式 1：2008-05-02 13:12:44 <br>
     * 格式 2：2008/05/02 13:12:44 <br>
     * 格式 3：2008年5月2日 13:12:44 <br>
     * 格式 4：2008年5月2日 13时12分44秒 <br>
     * 格式 5：2008年5月2日 星期五 13:12:44 <br>
     * 格式 6：2008年5月2日 星期五 13时12分44秒 <br>
     * 格式 7：20080502 <br>
     * 格式 8：20080502131244 <br>
     * 格式 9：2008-05-02 <br>
     * 格式 10：2008_05 <br>
     * 格式 11：2008 <br>
     * 格式 12：200805 <br>
     * 格式 13：2008-05 <br>
     * 格式 default：yyyyMMddHHmmss:20080502131244 <br>
     *
     * @param formatType
     * @param date
     * @return String
     */
    public static String get(int formatType, Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = null;
        switch (formatType) {
            case 1:
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                break;
            case 2:
                sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                break;
            case 3:
                sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                break;
            case 4:
                sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
                break;
            case 5:
                sdf = new SimpleDateFormat("yyyy年MM月dd日 E HH:mm:ss");
                break;
            case 6:
                sdf = new SimpleDateFormat("yyyy年MM月dd日 E HH时mm分ss秒");
                break;
            case 7:
                sdf = new SimpleDateFormat("yyyyMMdd");
                break;
            case 8:
                sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                break;
            case 9:
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                break;
            case 10:
                sdf = new SimpleDateFormat("yyyy_MM");
                break;
            case 11:
                sdf = new SimpleDateFormat("yyyy");
                break;
            case 12:
                sdf = new SimpleDateFormat("yyyyMM");
                break;
            case 13:
                sdf = new SimpleDateFormat("yyyy-MM");
                break;
            case 14:
                sdf = new SimpleDateFormat("MM-dd");
                break;
            case 15:
                sdf = new SimpleDateFormat("yyyy年MM月dd日");
                break;
            case 16:
                sdf = new SimpleDateFormat("ss");
                break;
            case 17:
                sdf = new SimpleDateFormat("yyyy年MM月");
                break;
            case 18:
                sdf = new SimpleDateFormat("yyyy/MM/dd");
                break;
            case 19:
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                break;
            default:
                sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                break;
        }
        sdf.setLenient(false);
        return sdf.format(date);
    }

    public static Date formatDate(int formatType, String date) {
        if (StringUtils.isBlank(date)) {
            return null;
        }
        SimpleDateFormat sdf = null;
        switch (formatType) {
            case 1:
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                break;
            case 2:
                sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                break;
            case 3:
                sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                break;
            case 4:
                sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
                break;
            case 5:
                sdf = new SimpleDateFormat("yyyy年MM月dd日 E HH:mm:ss");
                break;
            case 6:
                sdf = new SimpleDateFormat("yyyy年MM月dd日 E HH时mm分ss秒");
                break;
            case 7:
                sdf = new SimpleDateFormat("yyyyMMdd");
                break;
            case 8:
                sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                break;
            case 9:
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                break;
            case 10:
                sdf = new SimpleDateFormat("yyyy_MM");
                break;
            case 11:
                sdf = new SimpleDateFormat("yyyy");
                break;
            case 12:
                sdf = new SimpleDateFormat("yyyyMM");
                break;
            case 13:
                sdf = new SimpleDateFormat("yyyy-MM");
                break;
            case 14:
                sdf = new SimpleDateFormat("yyyy年MM月dd日");
                break;
            case 15:
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
                break;
            default:
                sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                break;
        }
        sdf.setLenient(false);
        try {
            return sdf.parse(date);
        } catch (Exception e) {

        }
        return null;
    }

    public static Date getDate(Date date, int calendarType, int fix) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendarType, calendar.get(calendarType) - fix);
        return calendar.getTime();
    }

    /**
     * 获取给入时间的凌晨时间
     * @author shamarfang
     * @param date
     * @return
     */
    public static Date getToday(Date date) {
        Calendar today = Calendar.getInstance();
        today.clear();
        Calendar temp = Calendar.getInstance();
        temp.setTime(date);
        today.set(Calendar.DAY_OF_MONTH, temp.get(Calendar.DAY_OF_MONTH));
        today.set(Calendar.MONTH, temp.get(Calendar.MONTH));
        today.set(Calendar.YEAR, temp.get(Calendar.YEAR));
        return today.getTime();
    }

    public static void main(String[] args) {
//        Date day0 = DateUtils.getToday(new Date());
//        Date endTime = DateUtils.getDate(day0, Calendar.DAY_OF_MONTH,1);
//        System.out.println(endTime);
//        Date startTime = DateUtils.getDate(endTime, Calendar.HOUR_OF_DAY,24);
//        System.out.println(startTime);
//        System.out.println(DateUtils.isNowBeforeDateMinute(DateUtils.formatDate(1, "2017-05-16 19:31:21"), 3));
        Date now = new Date();
        System.out.println(DateUtils.longOfTwoDate(DateUtils.formatDate(9, "2018-07-24"), DateUtils.formatDate(9, DateUtils.get(9, now))));

    }

    /**
     * 是否在当前之前几分钟
     *
     * @param date
     * @param minute
     * @return
     */
    public static boolean isNowBeforeDateMinute(Date date, int minute) {
        Date now = DateUtils.getDate(new Date(), Calendar.MINUTE, minute);
        return now.getTime() > date.getTime();
    }

    public static Date getInvalidTime(Date now, int timeOut) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        if (timeOut > 0) {
            calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + timeOut);
            return calendar.getTime();
        } else if (timeOut < 0) {
            int day = Math.abs(timeOut) - 1;
            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + day);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            return calendar.getTime();
        } else {
            return now;
        }
    }

    public static String TimeStamp2Date(String timestampString, String formats) {
        if (StringUtils.isBlank(formats))
            formats = "yyyy-MM-dd HH:mm:ss";
        Long timestamp;
        if (timestampString.length() == 10)
            timestamp = Long.parseLong(timestampString) * 1000;
        else
            timestamp = Long.parseLong(timestampString);
        return new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
    }

    public static int longOfTwoDate(Date first, Date second) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(first);
        int cnt = 0;
        while (calendar.getTime().compareTo(second) != 0) {
            calendar.add(Calendar.DATE, 1);
            cnt++;
        }
        return cnt;
    }

    public static Date string2Date(String dateTime, String format) {
        LocalDateTime localDateTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(format));
        return Date.from(localDateTime.atZone(zoneId).toInstant());
    }
}
